<template>
  <div>
    <div style="display: flex;">
      <el-tabs
        @tab-click="page.total=-1; aval=Number(state)>=0?{'state': Number(state)}:false;getData(aval)"
        v-model="state"
      >
        <el-tab-pane label="全部订单" name="-1"></el-tab-pane>
        <el-tab-pane
          v-for="i in states"
          :key="i.value"
          :label="i.label"
          :name="i.value.toString()"
        />
      </el-tabs>
      <el-tag
        style="margin-left: 10px;"
        type="info"
      >当前页订单金额:{{pageMoney.sum.CNY}}元 {{pageMoney.sum.point}}积分</el-tag>
      <el-tag
        style="margin-left: 10px;margin-right: 10px;"
        type="info"
      >实际支付:{{pageMoney.payment.CNY}}元 {{pageMoney.payment.point}}积分</el-tag>
      <el-button type="primary" style="height: 80%" @click="optdisplay=true">加入条件</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="ID" label="ID" width="40"></el-table-column>
      <el-table-column prop="name" label="商品" width="120" />
      <el-table-column prop="money.sum" label="金额" width="80">
        <template slot-scope="scope">
          {{scope.row.money.sum.CNY/100}}元
          <br />
          {{scope.row.money.sum.point}}积分
        </template>
      </el-table-column>
      <el-table-column prop="money.payment" label="实际支付" width="80">
        <template slot-scope="scope">
          {{scope.row.money.payment.CNY/100}}元
          <br />
          {{scope.row.money.payment.point}}积分
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="140">
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.state"
            placeholder="请选择"
            @change="upData({ID:scope.row.ID, state:scope.row.state})"
          >
            <el-option
              v-for="item in states"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="tradeNo" label="支付单号" width="120" />
      <el-table-column prop="name" label="姓名" width="80" />
      <el-table-column prop="tel" label="电话" width="120" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="createTime" label="下单时间" width="160" />
      <!-- <template slot-scope="scope">
          {{scope.row.createTime}}
        </template>
      </el-table-column>-->
      <el-table-column prop="changeTime" label="最后更改时间" width="160" />
    </el-table>
    <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
      :current-page="page.currentPage"
      :page-sizes="page.page_sizes"
      :page-size="page.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    ></el-pagination>
    <el-dialog title="条件选择" :visible.sync="optdisplay" width="500">
      <!-- @tab-click="" -->

      <el-tabs v-model="optval.opt">
        <el-tab-pane
          v-for="i in optmenu"
          :key="i.value"
          :label="i.label"
          :name="i.value.toString()"
        />
      </el-tabs>
      <el-date-picker
        v-model="optval.date"
        type="datetimerange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :default-time="['00:00:00', '23:59:59']"
        :picker-options="pickerOptions"
      />
      <el-select v-model="optval.address" placeholder="请选择">
        <el-option label="取消选择" :value="undefined" />
        <el-option
          v-for="item in addrs"
          :key="item.ID"
          :label="item.name"
          :value="item.name"
          :disabled="!item.state"
        />
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="optdisplay = false">取 消</el-button>
        <el-button type="primary" @click="optdisplay = false;mopt()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      states: [
        { value: 0, label: "已禁用" },
        { value: 1, label: "待支付" },
        { value: 2, label: "待配送" },
        { value: 3, label: "待收货" },
        { value: 4, label: "已完成" }
      ],
      tableData: [],
      page: {
        total: -1,
        page_sizes: [10, 20, 30, 40, 100, 200, 500, 1000],
        page_size: 10,
        currentPage: 1
      },
      state: "-1",
      aval: {},
      pageMoney: {
        sum: { CNY: 0, point: 0 },
        payment: { CNY: 0, point: 0 }
      },
      optdisplay: false,
      addrs: [],
      optval: {
        opt: "createTime"
      },
      optmenu: [
        { value: "createTime", label: "下单时间" },
        { value: "useTime", label: "支付时间" }
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(new Date(start.toDateString()).getTime());
              end.setTime(start.getTime() + (3600 * 24 - 1) * 1000);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(
                new Date(start.toDateString()).getTime() - 3600 * 1000 * 24
              );
              end.setTime(start.getTime() + (3600 * 24 - 1) * 1000);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },
  methods: {
    mopt() {
      if (this.optval.date) {
        this.aval.other =
          " and " +
          this.optval.opt +
          ">" +
          parseInt(this.optval.date[0] / 1000) +
          " and " +
          this.optval.opt +
          "<" +
          parseInt(this.optval.date[1] / 1000);
      }
      if (this.optval.address) {
        this.aval.address = "like " + this.optval.address + "%";
      }
      this.page.total = -1;
      this.getData();
      // console.log(this.$cp(this.optval));
      // console.log(this.$cp(this.aval));
    },
    // 时间戳转换成时间
    formatTime(time) {
      var date = new Date(time * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      return date.toLocaleString();
    },
    // 页面切换
    pageCurrentChange(val) {
      if (val == undefined) {
        this.page.currentPage = 1;
      } else {
        this.page.currentPage = val;
      }
      this.getData();
    },
    pageSizeChange(val) {
      if (val == undefined) {
        this.page.page_size = 10;
      } else {
        this.page.page_size = val;
      }
      this.pageCurrentChange(1);
    },
    // 获取数据
    getData() {
      this.pageMoney = {
        sum: { CNY: 0, point: 0 },
        payment: { CNY: 0, point: 0 }
      };
      let d = {
        cmd: 0,
        val: {
          token: sessionStorage.token,
          table: "orders"
        }
      };
      d.val.other = undefined;
      if (this.aval) d.val = Object.assign(d.val, this.aval);
      if (d.val.other) d.val.other += " ORDER BY changeTime DESC";
      else d.val.other = " ORDER BY changeTime DESC";
      if (this.page.total < 0) {
        d.val.sum = true;
        this.$axios.post("q.php", d).then(res => {
          // console.log(this.$cp(d.val));
          // console.log(this.$cp(res.data));
          if (res.data.result) {
            this.tableData = [];
            this.page.total = res.data.result.sum;
            this.getData();
          } else {
            this.$message({
              showClose: true,
              message: "当前数据为空",
              type: "error"
            });
          }
        });
        return;
      }
      if (this.page.page_size < 1000)
        d.val.limit =
          (this.page.currentPage - 1) * this.page.page_size +
          "," +
          this.page.page_size;
      this.$axios.post("q.php", d).then(res => {
        // console.log(this.$cp(res.data));
        if (res.data.result) {
          for (let k = 0; k < res.data.result.length; k++) {
            res.data.result[k].createTime = this.formatTime(
              res.data.result[k].createTime
            );
            res.data.result[k].changeTime = this.formatTime(
              res.data.result[k].changeTime
            );
            // console.log(res.data.result[k].money);
            this.pageMoney.sum.CNY += parseInt(
              res.data.result[k].money.sum.CNY
            );
            this.pageMoney.sum.point += parseInt(
              res.data.result[k].money.sum.point
            );
            this.pageMoney.payment.CNY += parseInt(
              res.data.result[k].money.payment.CNY
            );
            this.pageMoney.payment.point += parseInt(
              res.data.result[k].money.payment.point
            );
          }
          this.pageMoney.sum.CNY /= 100;
          this.pageMoney.payment.CNY /= 100;
          this.tableData = res.data.result;
        }
      });
    },
    upData(cmd) {
      if (!cmd || !cmd.ID) return;
      const data = {
        cmd: 1,
        val: {
          token: sessionStorage.token,
          table: "orders"
        }
      };
      data.val = Object.assign(data.val, cmd);
      this.$axios.post("q.php", data).then(res => {
        if (res.data.result) {
          this.$message({
            showClose: true,
            message: "修改数据成功",
            type: "success"
          });
        } else {
          this.$message({
            showClose: true,
            message: "修改数据失败!服务器繁忙或登陆过期",
            type: "error"
          });
        }
      });
    }
  },
  created() {
    this.$axios
      .post("q.php", {
        cmd: 0,
        val: { token: sessionStorage.token, table: "address" }
      })
      .then(res => {
        this.addrs = res.data.result;
      });
    this.pageSizeChange();
  }
};
</script>
<style scoped>
.aaa {
  overflow: hidden;
  word-break: keep-all !important;
}
</style>